import { _decorator, Camera, Node, Component, director, find, Vec3, v3, Tween, tween, SkeletalAnimation, math, JsonAsset, CapsuleColliderComponent } from 'cc';
import { EventManager, MyEvent } from './AshFramework/Managers/EventManager';
import Banner from './Banner';
import { Constant } from './AshFramework/Datas/Constant';
import PrefsUtil from './AshFramework/Utils/PrefsUtil';
import { HeroData, ItemData, PlayerData } from './Data';
import { DataManager } from './AshFramework/Managers/DataManager';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    private static _instance: GameManager = null;

    public static get Instance() {
        return this._instance;
    };

    static IsGameOver: boolean = false;
    static RandomLvs: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


    canvas: Node = null;

    get Lv() {
        return PrefsUtil.GetNumber(Constant.Key.Lv, 1);
    }
    set Lv(value: number) {
        console.log("Lv", value);
        PrefsUtil.SetNumber(Constant.Key.Lv, value);
    }

    private _money: number = 0;
    public get Money(): number {
        return this._money;
    }
    public set Money(value: number) {
        this._money = value;
        EventManager.Scene.emit(MyEvent.REFRESH_MONEY);
        GameManager.Instance.SaveData();
    }

    onLoad() {
        GameManager._instance = this;
        director.addPersistRootNode(this.node);

        // this.InitData();
        // PrefsUtil.ClearData();

        this.canvas = find("Canvas");

        this.Money = PrefsUtil.GetNumber(Constant.Key.Money);
    }

    protected start(): void {
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowBannerAd();
            Banner.Instance.ShowCustomAd();
        }
    }

    SaveData() {
        DataManager.SaveData();
        PrefsUtil.SetNumber(Constant.Key.Money, this.Money);
    }

    //获取已拥有的卡牌
    GetOwnerItems(): ItemData[] {
        return DataManager.itemDatas.filter(e => e.IsUnlock);
    }

    GetItemData(index: number): ItemData {
        return DataManager.itemDatas.find(e => e.Index == index);
    }

    IsEquipped(index: number) {
        return Boolean(DataManager.equippedItemDatas.find(e => e.Index == index));
    }

    SelectHero(index: number) {
        DataManager.SelectHero(index);
    }

    GetSelectedHeroIndex() {
        return DataManager.GetSelectedHeroIndex();
    }

    //#region 静态方法

    public static ReplayScene() {
        GameManager.Instance.SaveData();
        director.loadScene(director.getScene().name);
    }

    //#endregion

}